package com.pic.mgt.action.shell;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;

import com.pic.mgt.action.BusinessListSyncMgtAction;
import com.pic.mgt.telnetd.AbstractCommandPerformer;

public class BusinessListSyncMgtCommandPerformer extends AbstractCommandPerformer {
	
	private final String commandName = "bizsyc";
	private final String commandDescritpion = "business list synchronization management.";
	
	private BusinessListSyncMgtAction businessListSyncMgtAction;
	
	@Override
	protected void addOption(Options options) {
		options.addOption("syc", false, "do business list synchronization immediately.");
		options.addOption("add", false, "add business list synchronization schedule. (req: sn,ex ; opt: ds)");
		options.addOption("rmv", false, "remove business list synchronization schedule. (req: sn)");
		options.addOption("mod", false, "modify business list synchronization schedule time or schedule description. (req: sn ; opt: ex,ds)");
		options.addOption("lis", false, "list the business list synchronization schedule.");
		options.addOption("sn", true, "assign schedule name.");
		options.addOption("ds", true, "assign schedule description. (notice: put description in the middle of double quote.)");
		options.addOption("ex", true, "assign cron expression time of schedule. ex: \"0 * 14 * * ?\" (notice: double quote is required.)");
	}
	@Override
	protected String doCommandPerform(CommandLine commandLine) {
		if (commandLine.hasOption("syc")) {
			boolean isSynchronizSuccessful =
				this.businessListSyncMgtAction.synchronizeBusinessList();

			if (isSynchronizSuccessful) {
				return super.getCommandPerformFinishMsg();
			}
			else {
				return super.getCommandPerformFailedMsg();
			}
		}
		else if (commandLine.hasOption("add")) {
			if (!commandLine.hasOption("sn") ||
				!commandLine.hasOption("ex")) {
				return
				super.getReplyMessageWithReplyCode(
						"did not provide schedule name or cron schedule expression.",
						this.COMMAND_PERFORM_FAILED_CODE);
			}
			
			// -------------------------------------------
			
			boolean isAddScheduleSuccessful =
					this.businessListSyncMgtAction.addSyncBizListSchedule(
											commandLine.getOptionValue("sn"),
											commandLine.getOptionValue("ex"),
											commandLine.getOptionValue("ds"));
			
			if (isAddScheduleSuccessful) {
				return super.getCommandPerformFinishMsg();
			}
			else {
				return super.getCommandPerformFailedMsg();
			}
		}
		else if (commandLine.hasOption("rmv")) {
			if (!commandLine.hasOption("sn")) {
					return
					super.getReplyMessageWithReplyCode(
							"did not provide schedule name.",
							this.COMMAND_PERFORM_FAILED_CODE);
			}

			// -------------------------------------------

			boolean isRemoveScheduleSuccessful =
				this.businessListSyncMgtAction.removeSyncBizListSchedule(
											commandLine.getOptionValue("sn"));

			if (isRemoveScheduleSuccessful) {
				return super.getCommandPerformFinishMsg();
			}
			else {
				return super.getCommandPerformFailedMsg();
			}
		}
		else if (commandLine.hasOption("mod")) {
			if (!commandLine.hasOption("sn")) {
				return
				super.getReplyMessageWithReplyCode(
						"did not provide schedule name.",
						this.COMMAND_PERFORM_FAILED_CODE);
			}

			// -------------------------------------------

			boolean isModifyScheduleSuccessful =
				this.businessListSyncMgtAction.modifySyncBizListSchedule(
												commandLine.getOptionValue("sn"),
												commandLine.getOptionValue("ex"),
												commandLine.getOptionValue("ds"));

			if (isModifyScheduleSuccessful) {
				return super.getCommandPerformFinishMsg();
			}
			else {
				return super.getCommandPerformFailedMsg();
			}
		}
		else if (commandLine.hasOption("lis")) {
			String syncBizListScheduleInfoString =
				this.businessListSyncMgtAction.getSyncBizListScheduleInfoByBizName();
			
			if (null != syncBizListScheduleInfoString) {
				return getReplyMessageWithReplyCode(syncBizListScheduleInfoString,
						this.COMMAND_PERFORM_SUCCESS_CODE);
			}
			else {
				return super.getCommandPerformFailedMsg();
			}
		}
		else {
			return
			super.getReplyMessageWithReplyCode(
					"did not specify management action (option: syc, add, rmv or mod).",
					this.COMMAND_PERFORM_FAILED_CODE);
		}
	}
	@Override
	public String getCommandDescription() {
		return this.commandDescritpion;
	}
	@Override
	public String getCommandName() {
		return this.commandName;
	}
	
	// --- getter and setter ---
	
	public void setBusinessListSyncMgtAction(
			BusinessListSyncMgtAction businessListSyncMgtAction) {
		this.businessListSyncMgtAction = businessListSyncMgtAction;
	}


}
